﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

/// <summary>
/// A field generator which creates dynamic gridview columns.
/// </summary>
public class LanguageColumnGenerator : IAutoFieldGenerator {
    private string[] languages;
    private string defaultLanguage;
    private bool showGoogleTranslateButton;
    private string defaultLanguageCode = string.Empty; // no good if the destination language is supported but the default language isn't

    /// <summary>
    /// Initializes a new <see cref="LanguageColumnGenerator"/> to generate columns for the specified languages.
    /// </summary>
    /// <param name="languages">The languages for which to generate columns.</param>
    public LanguageColumnGenerator(string[] languages) : this(languages, "", false) { }

    /// <summary>
    /// Initializes a new <see cref="ColGen"/> to generate columns for the specified languages.
    /// </summary>
    /// <param name="languages">The languages for which to generate columns.</param>
    /// <param name="defaultLanguage"><c>true</c> if this column is for the default language.</param>
    /// <param name="showGoogleTranslateButton"><c>true</c> if the Google translate button should be shown when the row is in edit mode.</param>
    public LanguageColumnGenerator(string[] languages, string defaultLanguage, bool showGoogleTranslateButton) {
        this.languages = languages;
        this.defaultLanguage = defaultLanguage;
        this.showGoogleTranslateButton = showGoogleTranslateButton;
        if(showGoogleTranslateButton && !string.IsNullOrEmpty(defaultLanguage)) {
            try {
                string tempCode = Utils.ISOCode2GoogleCode(CultureInfo.GetCultureInfo(defaultLanguage).TwoLetterISOLanguageName);
                if(tempCode.Length > 0) {
                    this.defaultLanguageCode = tempCode;
                }
            } catch { }
        }
    }

    #region IAutoFieldGenerator Members

    System.Collections.ICollection IAutoFieldGenerator.GenerateFields(Control control) {
        List<TemplateField> fields = new List<TemplateField>();
        string friendlyName,
               isoCode = null,
               tempCode;
        foreach(string lang in languages) {
            TemplateField fld = new TemplateField();
            fld.SortExpression = lang;
            try {
                CultureInfo info = CultureInfo.GetCultureInfo(lang);
                friendlyName = info.EnglishName;
                tempCode = info.TwoLetterISOLanguageName;
            } catch {
                friendlyName = lang;
                tempCode = string.Empty;
            }
            if(showGoogleTranslateButton && lang != defaultLanguage) {
                if(defaultLanguageCode == null) {// default lang not supported by Google
                    isoCode = string.Empty;
                } else {
                    isoCode = Utils.ISOCode2GoogleCode(tempCode);
                }
            }
            fld.HeaderText = friendlyName + "<br />(<small>" + lang + "</small>)";
            fld.ItemTemplate = new GridViewTemplate(ListItemType.Item, lang, friendlyName, lang == defaultLanguage, isoCode);
            fld.ItemStyle.CssClass = "ssCell";
            fields.Add(fld);
        }
        return fields;
    }

    #endregion
}
